package com.blvtphan.tipocketclubs.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.content.Context;

public class DateTimeUtils {
	
    public static String formatStandarDate(long date)
	{
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	return sdf.format(date);
	}
    
    public static String formatDateFullMonth(long date)
	{
    	SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy");
    	//Test duplicate time
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	return sdf.format(new Date(date));
	}
    
    //Get from server
    public static long formatLongStandarDate(long date)
	{
//    	String dateFormat = PreferenceUtils.getString(context, Constant.PrefenceKey.PREF_DATE_FORMAT, "dd/MM/yyyy");
//    	if(StringUtils.equals(dateFormat, "")){
//    		dateFormat = "dd/MM/yyyy";
//    	}
//    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	
    	// UTC
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        // Local
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
    	// Set Time Zone: GMT
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	sdf.setTimeZone(TimeZone.getDefault());
    	
    	// Adjust locale and zone appropriately
    	Date outputDate = null;
		try {
			outputDate = inputFormat.parse(inputFormat.format(new Date(date)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	//return new Date(sdf.format(new Date(date))).getTime();
		return new Date(sdf.format(outputDate)).getTime();
	}
    
    public static String formatMonthYearDate(Date date)
    {
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM, yyyy");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	return sdf.format(date);
    }
    
    public static String formatFullDateTime(Date date)
    {
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy KK:mm:ss a");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	return sdf.format(date);
    }
    
    public static String formatFullTime(Date date)
    {
    	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	return sdf.format(date);
    }
    
    //Get from server
    public static String getTimeHourAndMinute(long date)
    {
    	// UTC
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        // Local
    	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    	// Set Time Zone: GMT
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	sdf.setTimeZone(TimeZone.getDefault());
    	
    	// Adjust locale and zone appropriately
    	Date outputDate = null;
		try {
			outputDate = inputFormat.parse(inputFormat.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
    	return sdf.format(outputDate);
    }
    
    /**
     * Get Hour and Minute with AM and PM format
     * @param date
     * @return
     */
    public static String getTimeHourAndMinuteAmPm(long date)
    {
    	// UTC
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        // Local
    	SimpleDateFormat sdf = new SimpleDateFormat("K:mm a");
    	// Set Time Zone: GMT
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	sdf.setTimeZone(TimeZone.getDefault());
    	
    	// Adjust locale and zone appropriately
    	Date outputDate = null;
		try {
			outputDate = inputFormat.parse(inputFormat.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
    	return sdf.format(outputDate);
    }
    
    /**
     * @param date
     * @return
     */
    public static String getFullDateTime(Context context, long date)
    {
        // Local
    	String dateFormat = "dd/MM/yyyy";
    	
    	// UTC
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        // Local
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat + " KK:mm a");
    	// Set Time Zone: GMT
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	sdf.setTimeZone(TimeZone.getDefault());
    	
    	// Adjust locale and zone appropriately
    	Date outputDate = null;
		try {
			outputDate = inputFormat.parse(inputFormat.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
    	return sdf.format(outputDate);
    }
    
    public static long addTimeToDate(long date, String time)
    {
    	String sDate = formatStandarDate(date) + " " + time;
    	Date newDate = new Date(sDate);
    	//SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	//Date convertDate = new Date(sdf.format(newDate));
    	//return convertDate.getTime();
    	return newDate.getTime();
    }
    
    public static long getLongTimeGMT(String sDate)
    {
//    	String dateFormat = PreferenceUtils.getString(context, Constant.PrefenceKey.PREF_DATE_FORMAT, "dd/MM/yyyy");
//    	if(StringUtils.equals(dateFormat, "")){
//    		dateFormat = "dd/MM/yyyy";
//    	}
//    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
    	Date date = new Date(sDate);
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	String nDate = sdf.format(date);
    	return new Date(nDate).getTime();
    }
    
    public static Date getSimpleFormatDate(Date sDate)
    {
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	String nDate = sdf.format(sDate.getTime());
    	return new Date(nDate);
    }
    
    /**
     * Convert UTC Time to Local Time
     * - First, set format with Locale.US
     * - Second, set UTC TimeZone
     * 
     * @param context
     * @param date
     * @return
     */
    public static String formatDateShowUI(Context context, Date date)
    {
    	// UTC
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        // Local
    	String dateFormat = "dd/MM/yyyy";
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	sdf.setTimeZone(TimeZone.getDefault());
    	
    	// Adjust locale and zone appropriately
    	Date outputDate = null;
		try {
			outputDate = inputFormat.parse(inputFormat.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return sdf.format(outputDate);
    }
    
    public static String formatDateShowUI(Context context, long date)
    {
    	// UTC
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        // Local
    	String dateFormat = dateFormat = "dd/MM/yyyy";
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	sdf.setTimeZone(TimeZone.getDefault());
    	
    	// Adjust locale and zone appropriately
    	Date outputDate = null;
		try {
			outputDate = inputFormat.parse(inputFormat.format(new Date(date)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
    	return sdf.format(outputDate);
    }
    
    /**
     * Convert String Date to Date with standard format
     * Use for: Show Report (filter date)
     * 
     * @param context
     * @param date
     * @return
     */
    public static Date convertFormatDate(Context context, String date) {
    	String dateFormat = dateFormat = "dd/MM/yyyy";
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	SimpleDateFormat sdfStandard = new SimpleDateFormat("MMM dd, yyyy");
    	String rsDate = "";
    	try {
            rsDate = sdfStandard.format(sdf.parse(date));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    	return new Date(rsDate);
    }
    
    public static String convertFormatDateToString(Context context, String date) {
    	String dateFormat = "yyyy-MM-dd";
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	SimpleDateFormat sdfStandard = new SimpleDateFormat("dd-MMM-yyyy");
    	String rsDate = "";
    	try {
            rsDate = sdfStandard.format(sdf.parse(date));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    	return rsDate;
    }
    
    /**
     * Convert dd/MM/yyyy to MMM dd, yyyy to compare. 
     * Use for: sort current plan
     * 
     * @param date
     * @return
     */
    public static Date convertFormatDateFromDdMmYyyy(String date) {
    	String dateFormat = "dd/MM/yyyy";
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	SimpleDateFormat sdfStandard = new SimpleDateFormat("MMM dd, yyyy");
    	String rsDate = "";
    	try {
            rsDate = sdfStandard.format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    	return new Date(rsDate);
    }
    
    public static Date formatLongStandardDate(long date)
	{
    	SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
    	//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	return new Date(sdf.format(new Date(date)));
	}
    
    /**
     * Convert Local Time to UTC Time to Submit
     * 
     * @param date
     * @return
     */
    public static long convertLocalTimeToUTC(Date date) {
    	// UTC
        SimpleDateFormat outFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        outFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
		return new Date(outFormat.format(date)).getTime();
    }
    
    public static long convertLocalTimeToUTC(long date) {
    	// UTC
        SimpleDateFormat outFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
        outFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
		return new Date(outFormat.format(new Date(date))).getTime();
    }
    
    /**
     * Get tomorrow date
     */
    public static Date getTomorrowDate() {
    	Calendar calendar = Calendar.getInstance();
    	calendar.add(Calendar.DATE, 1);
    	return calendar.getTime();
    }
}
